package cn.stimd.spring.transaction.support;

/**
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class DefaultTransactionStatus extends AbstractTransactionStatus {
    //底层的事务对象
    private final Object transaction;
    //当前事务被挂起的资源
    private final Object suspendedResources;
    //是否为新事务，否则是已存在事务的一部分（空事务也算新事务）
    private final boolean newTransaction;
    private final boolean newSynchronization;
    //是否为只读事务
    private final boolean readOnly;

    public DefaultTransactionStatus(Object transaction, Object suspendedResources, boolean newTransaction, boolean newSynchronization, boolean readOnly) {
        this.transaction = transaction;
        this.suspendedResources = suspendedResources;
        this.newTransaction = newTransaction;
        this.newSynchronization = newSynchronization;
        this.readOnly = readOnly;
    }

    public Object getTransaction() {
        return this.transaction;
    }

    public Object getSuspendedResources() {
        return suspendedResources;
    }

    public boolean isNewSynchronization() {
        return newSynchronization;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public boolean hasTransaction(){
        return this.transaction != null;
    }

    @Override
    public boolean isNewTransaction() {
        return hasTransaction() && this.newTransaction;
    }


    @Override
    public boolean isGlobalRollbackOnly() {
        return ((this.transaction instanceof SmartTransactionObject) && ((SmartTransactionObject) this.transaction).isRollbackOnly());
    }
}
